import './interfaces';

const systemid = '1094';

async function query(tablename: string, fields: string[], filter: any, sort: any, page_no: number, page_size: number) {
	const q = [{
		criteria: filter,
		fields: fields.reduce((pre, cur) => {
			pre[cur] = 1;
			return pre;
		}, {}),
		page_no,
		page_size,
		query_mode: 1,
		sort,
		spaceid,
		sql: '',
		systemid,
		tablename
	}];
	return (await send_msg('dbacc.find.intranet', q))[0].dataArray as any[];
}

async function count(tablename: string, criteria: any) {
	const c = [{
		criteria,
		query_mode: 1,
		spaceid,
		systemid,
		tablename
	}];
	return (await send_msg('dbacc.count.intranet', c))[0].count as number;
}

async function atom(msg: {
	data: {
		criteria: object;
		page_no: number,
		page_size: number,
		sort: object
	};
}, headers: IHeaders) {
	// 1. 根据data查询H5内容
	const ret = await query('h5_playback_content',
		['cont_des', 'cont_no', 'cont_address'], msg.data.criteria, msg.data.sort, msg.data.page_no, msg.data.page_size) as object[];
	const cut = await count('h5_playback_content', msg.data.criteria);
	const obj = { dataArray: ret, count: cut };
	return obj;
}
